package com.sxbbc.common.core.utils;

import java.io.*;
import java.util.Properties;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.sxbbc.common.basics.enums.RandNumType;
import com.sxbbc.common.basics.files.RestUploadFileInfo;
import com.sxbbc.common.basics.files.UploadUtils;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.encrypts.des.DESEncode;
import com.sxbbc.common.basics.utils.qr.QRCodeUtil;
import com.sxbbc.common.basics.utils.radoms.RandNumUtils;
import com.sxbbc.common.core.enums.ScanQrEnums;
import org.apache.poi.util.StringUtil;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import com.aliyun.oss.event.ProgressListener;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * 文件综合
 * @author Wahyee
 */
public class FileUtils implements ProgressListener {

	private static   String  endpoint = "";
	private static   String accessKeyId = "";
	private static   String accessKeySecret = "";
	private static   String bucketName ="";
	private static   String rootPath="";
	private static 	 String fullPath="";

	static {
		try {
			Properties properties = PropertiesLoaderUtils.loadAllProperties("alioss.properties");
			endpoint = new String(properties.getProperty("ENDPOINT").getBytes("iso-8859-1"), "utf8");
			accessKeyId = new String(properties.getProperty("ACCESSKEY_ID").getBytes("iso-8859-1"), "utf8");
			accessKeySecret = new String(properties.getProperty("ACCESSKEY_SECRET").getBytes("iso-8859-1"), "utf8");
			bucketName=new String(properties.getProperty("BUCKET_NAME").getBytes("iso-8859-1"), "utf8");
			rootPath=new String(properties.getProperty("ROOT_PATH").getBytes("iso-8859-1"), "utf8");
			fullPath=new String(properties.getProperty("FULL_PATH").getBytes("iso-8859-1"), "utf8");
		} catch (Exception e) {

		}
	}



	/**
	 * 
	 * 获取商家收款二维码
	 * @param bid
	 * @return 收款码图片地址
	 * @author Wahyee
	 */
	public static RestUploadFileInfo getQRCodeBusinessPay(Integer bid) {
        InputStream is = QRCodeUtil.getInstance().encode(DESEncode.getInstance().
                encrypt("type:"+ScanQrEnums.OFFLINE_BUS.getType()+", data:"+ bid));
        String filename = DateUtil.getInstance().currentTimeMillis() +
                RandNumUtils.getInstance().get(RandNumType.NUMBER, 5) + "." + QRCodeUtil.getInstance().FORMAT_NAME;
        return  UploadUtils.getInstance().uploadFile(is, filename);
	}





	//流文件上传
	public static String up(File file) {

		try {
			String fileName = file.getName();
			String key = getUUID()+fileName.substring(fileName.lastIndexOf("."));
			InputStream inputStream= new FileInputStream(file);
			// 创建OSSClient实例。
			OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
			// 上传文件流。
			ossClient.putObject(bucketName,rootPath+key, inputStream);
			// 关闭OSSClient。
			ossClient.shutdown();
			return fullPath+"/"+rootPath+key;
		}catch (Exception e){
			return null;
		}


	}


	//TODO 富文本图片上传中需要重寫文件上傳的方法
	public static Object uploadFile(MultipartFile file) {
		JSONObject json = new JSONObject();
		if (file != null) {
			try {
				RestUploadFileInfo result =UploadUtils.getInstance().uploadFileQN(file);
				if (!result.isStatus()) {
					json.put("state", "FAIL");
				} else {
					json.put("state", "SUCCESS");
					json.put("url", result.getData());
					json.put("size", file.getSize());
					json.put("original", file.getOriginalFilename());
					json.put("title", file.getOriginalFilename());
					json.put("type", file.getContentType());
				}
			} catch (Exception e) {
				json.put("state", "FAIL");
			}
		}
		return json;
	}



	/**
	 * 通过key值删除文件
	 * @param key
	 */
	public  static void deleteFile(String key){
		// 创建OSSClient实例。
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
		// 删除文件。如需删除文件夹，请将ObjectName设置为对应的文件夹名称。如果文件夹非空，则需要将文件夹下的所有object删除后才能删除该文件夹。
		ossClient.deleteObject(bucketName, key);
		// 关闭OSSClient。
		ossClient.shutdown();
	}


	@Override
	public void progressChanged(ProgressEvent progressEvent) {

	}


	public  static String getUUID(){
		String uuid = UUID.randomUUID().toString();
		return uuid = uuid.replace("-", "");
	}
}
